/*
 * BoardSolverPanel.java
 *
 * Created on January 30, 2008, 9:03 AM
 */

package gui.mainwindow;

import competition.CompetitionExecutor;
import java.util.Collections;
import java.util.List;
import scrabblehelper.StaticFields;
import scrabbletools.Board;
import scrabbletools.BoardAnagramUtils;
import scrabbletools.WordPlacement;

/**
 *
 * @author  Nick
 */
public class BoardSolverPanel extends javax.swing.JPanel {
    
    /** Creates new form BoardSolverPanel */
    public BoardSolverPanel() {
        initComponents();
        
        scrabbleBoardPanel.addScrabbleBoardListener(wordPlacementList);
        wordPlacementList.getModel().addListDataListener(scrabbleBoardPanel);
        wordPlacementList.addListSelectionListener(scrabbleBoardPanel);
        wordPlacementList.addMouseListener(scrabbleBoardPanel.getMouseListenerForWordPlacementList());
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrabbleBoardPanel = new gui.ScrabbleBoardPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        wordPlacementList = new gui.WordPlacementList();
        rackLettersField = new gui.RackLettersTextField();
        solveButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        processTimeLabel = new javax.swing.JLabel();
        uniquePlacementsLabel = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        wordPlacementList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(wordPlacementList);

        rackLettersField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rackLettersFieldActionPerformed(evt);
            }
        });

        solveButton.setText("Solve It!");
        solveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                solveButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("(Enter blanks as \"0\")");

        jLabel2.setText("Number of unique placements:");

        jLabel3.setText("Time to process:");

        processTimeLabel.setText(" ");

        uniquePlacementsLabel.setText(" ");

        jButton1.setText("Compete!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrabbleBoardPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .addComponent(rackLettersField, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(solveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(processTimeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(uniquePlacementsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jSeparator1)
                    .addComponent(scrabbleBoardPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rackLettersField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(solveButton)
                            .addComponent(jButton1)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(uniquePlacementsLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(processTimeLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void solveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_solveButtonActionPerformed
        solve();
}//GEN-LAST:event_solveButtonActionPerformed

    private void rackLettersFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rackLettersFieldActionPerformed
        solve();
    }//GEN-LAST:event_rackLettersFieldActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        new CompetitionExecutor(2, scrabbleBoardPanel).playWholeGame();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    public void solve() {
        char[][] boardLetters = scrabbleBoardPanel.getCharArray();
        char[] rackLetters = rackLettersField.getText().toCharArray();
        
        BoardAnagramUtils utils = new BoardAnagramUtils(new Board(boardLetters));
        utils.setDictionary(StaticFields.getDictionary());
        utils.setRackLetters(rackLetters);
        
        long startTime = System.currentTimeMillis();
        List<WordPlacement> words = utils.findAllBoardPossibilities();
        long time = System.currentTimeMillis() - startTime;
        
        System.out.println("Time to compute:  " + time + "ms");
        
        uniquePlacementsLabel.setText(words.size() + "");
        processTimeLabel.setText(time + "ms");
        
        displayWords(words);
    }
    
    public void displayWords(List<WordPlacement> words) {
        Collections.sort(words);
        
        wordPlacementList.getModel().clear();
        wordPlacementList.getModel().addAll(words);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel processTimeLabel;
    private gui.RackLettersTextField rackLettersField;
    private gui.ScrabbleBoardPanel scrabbleBoardPanel;
    private javax.swing.JButton solveButton;
    private javax.swing.JLabel uniquePlacementsLabel;
    private gui.WordPlacementList wordPlacementList;
    // End of variables declaration//GEN-END:variables
    
}
